<?php
/**
 * User:于小涛
 * Date:2020/2/17
 * Time:15:37
 */
namespace app\admin\controller\rbac;

use app\admin\BaseController;
use app\admin\model\rbac\Admin as AdminModel;
use app\admin\business\rbac\Admin as AdminBusiness;
use think\facade\View;

class Admin extends BaseController {

    public function getList(){
        return view();
    }

    public function getListData(){
        $obj_admin_model = new AdminModel();
        $arr_admin_info=session('admin');
        switch ($arr_admin_info['type']){
            case 0:
                $arr_where_type=array('type','>=',0);
                break;
            case 1:
                $arr_where_type=array('type','>=',1);
                break;
            default:
                $arr_where_type=array('type','>=',2);
        }
        $arr_where=array(
            array('is_del','=',1),
            $arr_where_type,
            array('name','like',"%{$this->request->param('name')}%"),
        );
        $arr_list=$obj_admin_model->getList($arr_where,$this->request->param('page'),$this->request->param('limit'));
        //查询每个管理员属于的角色
        foreach($arr_list['data'] as &$arr_item){
            $arr_item['role_names']='';
            $str_sql="select rr.* from rbac_admin_role as rar left join rbac_role as rr on rr.id=rar.role_id where rar.admin_id=".$arr_item['id'];
            $arr_role_list=$obj_admin_model->query($str_sql);
            if($arr_role_list){
                $arr_role=[];
                foreach($arr_role_list as $arr_role_item){
                    $arr_role[]=$arr_role_item['name'];
                }
                $arr_item['role_names']=implode('、',$arr_role);
            }
        }
        return output_page($arr_list);
    }

    public function add(){
        if($this->request->param('name')){
            //如果消息推送验证码不为空，则要获取当前用户的微信账号关注的消息推送公众号的消息
            $str_wechat_unionid='';
            if(!empty($this->request->param('verify_code'))){
                $str_url=config("project.message_push_api").'?m=Message&c=PlatformPushMessage&a=getWechatUserInfo';
                $arr_post_data=array(
                    'platform_code'=>config("project.message_push_platform_code"),
                    'verify_code'=>$this->request->param('verify_code')
                );
                trace("给消息推送平台发送消息，地址：{$str_url},数据：".var_export($arr_post_data,true),'info');
                $arr_platform_re=curl_post($str_url,$arr_post_data);
                $arr_platform_re['data']=json_decode($arr_platform_re['data'],true);
                if($arr_platform_re['data']['ret']!=0){
                    return output_data(array(),'error',$arr_platform_re['data']['reason']);
                }
                $str_wechat_unionid=$arr_platform_re['data']['data']['unionid'];
            }
            $arr_data_add=array(
                'name'=>$this->request->param('name'),
                'account'=>$this->request->param('account'),
                'verify_code'=>$this->request->param('verify_code'),
                'wechat_unionid'=>$str_wechat_unionid,
                'password'=>md5($this->request->param('password').$this->request->param('password')),
                'type'=>$this->request->param('type')??2,
                'token'=>md5($this->request->param('account').rand(1,999999999)),
            );
            $obj_admin_model=new AdminModel();
            $int_id=$obj_admin_model->add($arr_data_add);
            if($int_id){
                return output_data();
            }else{
                return output_data(array(),'error');
            }
        }else{
            return view();
        }
    }

    public function edit(){
        $obj_admin_model=new AdminModel();
        if($this->request->param('name')){
            $arr_data_edit=array(
                'name'=>$this->request->param('name'),
                'type'=>$this->request->param('type')??2,
                'token'=>md5($this->request->param('account').rand(1,999999999)),
            );
            if(!empty($this->request->param('verify_code'))){
                $str_url=config("project.message_push_api").'?m=Message&c=PlatformPushMessage&a=getWechatUserInfo';
                $arr_post_data=array(
                    'platform_code'=>config("project.message_push_platform_code"),
                    'verify_code'=>$this->request->param('verify_code')
                );
                trace("给消息推送平台发送消息，地址：{$str_url},数据：".var_export($arr_post_data,true),'info');
                $arr_platform_re=curl_post($str_url,$arr_post_data);
                $arr_platform_re['data']=json_decode($arr_platform_re['data'],true);
                if($arr_platform_re['data']['ret']!=0){
                    return output_data(array(),'error',$arr_platform_re['data']['reason']);
                }
                $arr_data_edit['verify_code']=$this->request->param('verify_code');
                $arr_data_edit['wechat_unionid']=$arr_platform_re['data']['data']['unionid'];
            }
            if($this->request->param('password')){
                $arr_data_edit['password'] = md5($this->request->param('password').$this->request->param('password'));
            }
            $int_num=$obj_admin_model->edit($this->request->param('id'),$arr_data_edit);
            if($int_num){
                return output_data();
            }else{
                return output_data(array(),'error');
            }
        }else{
            $arr_admin_info=$obj_admin_model->find($this->request->param('id'));
            View::assign('arr_data_info',$arr_admin_info);
            return view();
        }
    }

    public function lock(){
        $obj_admin_model=new AdminModel();
        $arr_data_edit=array(
            'is_lock'=>$this->request->param('is_lock')??2,
        );
        $int_num=$obj_admin_model->edit($this->request->param('id'),$arr_data_edit);
        if($int_num){
            return output_data();
        }else{
            return output_data(array(),'error');
        }
    }

    public function del(){
        $obj_admin_model=new AdminModel();
        $arr_data_edit=array(
            'is_del'=>2,
        );
        $int_num=$obj_admin_model->edit($this->request->param('id'),$arr_data_edit);
        if($int_num){
            return output_data();
        }else{
            return output_data(array(),'error');
        }
    }

    public function configRole(){
        if($this->request->param('admin_id')){
            $obj_admin_business = new AdminBusiness();
            $obj_admin_business->configRole($this->request->param('admin_id'),array_unique($this->request->param('roleids')));
            return output_data();
        }else{
            return view();
        }
    }
    public function changePassword(){
        if($this->request->param('old_password')){
            $arr_admin_info=session('admin');
            if($arr_admin_info['password']!=md5($this->request->param('old_password').$this->request->param('old_password'))){
                return output_data(array(),'error','旧密码不对');
            }
            if($this->request->param('password1')!=$this->request->param('password2')){
                return output_data(array(),'error','两次密码不一致');
            }
            $arr_edit=[
                'password'=>md5($this->request->param('password1').$this->request->param('password1')),
            ];
            $obj_admin_model=new AdminModel();
            $int_num=$obj_admin_model->edit($arr_admin_info['id'],$arr_edit);
            if($int_num){
                $arr_admin_info['password']=$arr_edit['password'];
                session('admin',$arr_admin_info);
                op_log("rbac",'edit',"修改密码");
                return output_data();
            }else{
                return output_data(array(),'error','修改密码失败');
            }
        }else{
            return view();
        }
    }
}